﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Ctrls;
using System.Collections.ObjectModel;
using System.Text;

namespace CommunitySystem.PageView.晋升管理
{
    public partial class AddEmpWindow : ChildWindow
    {
        private JinShengService.JinShengServiceClient _Client = null;
        private MouseTool _mouseTool = new MouseTool();
        private int _OrgID = 0;
        private JinShengService.JinShen_MainInfo _MainInfo = null;
        public AddEmpWindow(int OrgID, JinShengService.JinShen_MainInfo mainInfo)
        {
            InitializeComponent();
            _MainInfo = mainInfo;
            _OrgID = OrgID;
            tbCondition.AddHandler(TextBox.MouseLeftButtonDownEvent, new MouseButtonEventHandler(TextBox_MouseLeftButtonDown), true);
            _Client = new JinShengService.JinShengServiceClient();
           
            
           // _Client.GetEmpBasicInfoCompleted += new EventHandler<JinShengService.GetEmpBasicInfoCompletedEventArgs>(_Client_GetEmpBasicInfoCompleted);
            _Client.GetEmpBasicInfo_zjCompleted += new EventHandler<JinShengService.GetEmpBasicInfo_zjCompletedEventArgs>(_Client_GetEmpBasicInfo_zjCompleted);
           
           // _Client.GetEmpBasicInfoAccordingToDutyCompleted += new EventHandler<JinShengService.GetEmpBasicInfoAccordingToDutyCompletedEventArgs>(_Client_GetEmpBasicInfoAccordingToDutyCompleted);
            //整建修改
            _Client.GetEmpBasicInfoAccordingToDuty_ZJCompleted += new EventHandler<JinShengService.GetEmpBasicInfoAccordingToDuty_ZJCompletedEventArgs>(_Client_GetEmpBasicInfoAccordingToDuty_ZJCompleted);
           
            _Client.GetOrgForHRAsync(EHR.Common.Session.LoginUser.LoginName);
            _Client.GetOrgForHRCompleted += (sender, e) =>
            {
                if (JinShenConst.Administrator.Contains(e.Result))
                {
                    btnAdminSearch.Visibility = System.Windows.Visibility.Visible;
                    //方案为竞争上岗，单位只有一个人，管理员可进行操作添加此人
                    if (mainInfo.Status == "审核通过" && mainInfo.PromotType == "竞争上岗")
                    {
                       
                        btnAdminSearch.Visibility = System.Windows.Visibility.Collapsed;
                        btnCommonSearch.Visibility = System.Windows.Visibility.Visible;
                    }
                }
                if (JinShenConst.HR.Contains(e.Result))
                {
                   
                    btnCommonSearch.Visibility = System.Windows.Visibility.Visible;
                }
            };
        }

        void _Client_GetEmpBasicInfo_zjCompleted(object sender, JinShengService.GetEmpBasicInfo_zjCompletedEventArgs e)
        {
            lb.ItemsSource = null;
            lb.ItemsSource = e.Result;
        }

        void _Client_GetEmpBasicInfoAccordingToDuty_ZJCompleted(object sender, JinShengService.GetEmpBasicInfoAccordingToDuty_ZJCompletedEventArgs e)
        {
            lb.ItemsSource = null;
            lb.ItemsSource = e.Result;
        }
        private bool _ISsh=false;
        public AddEmpWindow(int OrgID, JinShengService.JinShen_MainInfo mainInfo,bool IsSH)
        {
            InitializeComponent();
            _ISsh = IsSH;
            _MainInfo = mainInfo;
            _OrgID = OrgID;
            tbCondition.AddHandler(TextBox.MouseLeftButtonDownEvent, new MouseButtonEventHandler(TextBox_MouseLeftButtonDown), true);
            _Client = new JinShengService.JinShengServiceClient();
            // _Client.GetEmpBasicInfoCompleted += new EventHandler<JinShengService.GetEmpBasicInfoCompletedEventArgs>(_Client_GetEmpBasicInfoCompleted);
            _Client.GetEmpBasicInfo_zjCompleted += new EventHandler<JinShengService.GetEmpBasicInfo_zjCompletedEventArgs>(_Client_GetEmpBasicInfo_zjCompleted);

            // _Client.GetEmpBasicInfoAccordingToDutyCompleted += new EventHandler<JinShengService.GetEmpBasicInfoAccordingToDutyCompletedEventArgs>(_Client_GetEmpBasicInfoAccordingToDutyCompleted);
            //整建修改
            _Client.GetEmpBasicInfoAccordingToDuty_ZJCompleted += new EventHandler<JinShengService.GetEmpBasicInfoAccordingToDuty_ZJCompletedEventArgs>(_Client_GetEmpBasicInfoAccordingToDuty_ZJCompleted);
           
            
            _Client.GetOrgForHRAsync(EHR.Common.Session.LoginUser.LoginName);
            _Client.GetOrgForHRCompleted += (sender, e) =>
            {
                if (JinShenConst.Administrator.Contains(e.Result))
                {
                    btnAdminSearch.Visibility = System.Windows.Visibility.Visible;
                }
                if (JinShenConst.HR.Contains(e.Result))
                {
                    btnCommonSearch.Visibility = System.Windows.Visibility.Visible;
                }
            };
        }

        void _Client_GetEmpBasicInfoAccordingToDutyCompleted(object sender, JinShengService.GetEmpBasicInfoAccordingToDutyCompletedEventArgs e)
        {
            lb.ItemsSource = null;
            lb.ItemsSource = e.Result;
        }

        void _Client_GetEmpBasicInfoCompleted(object sender, JinShengService.GetEmpBasicInfoCompletedEventArgs e)
        {
            lb.ItemsSource = null;
            lb.ItemsSource = e.Result;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            SelectItems = null;
            var source = lb.ItemsSource as System.Collections.ObjectModel.ObservableCollection<CommunitySystem.JinShengService.Emp_BasicInfoModel>;
            SelectItems = new ObservableCollection<JinShengService.Emp_BasicInfoModel>(source.Where(c => c.IsSelected == true));
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void TextBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!string.IsNullOrEmpty(tbCondition.Text) && tbCondition.Text.Equals("请输入姓名活证件号码"))
            {
                tbCondition.SelectAll();
            }
        }

        private void tbCondition_KeyDown(object sender, KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty(tbCondition.Text) && tbCondition.Text.Equals("请输入姓名活证件号码"))
            {
                tbCondition.FontStyle = FontStyles.Normal;
                //tbCondition.Text = e.Key.ToString();
                tbCondition.Foreground = new SolidColorBrush(Colors.Black);
            }
        }

        private void ImageButton_LostFocus(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(tbCondition.Text))
            {
                tbCondition.Text = "请输入姓名活证件号码";
                tbCondition.FontStyle = FontStyles.Italic;
                tbCondition.Foreground = new SolidColorBrush(Colors.LightGray);
            }
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //正科长   (乡科级正职  领导职务)       正科级或副科级满二年
            //主任科员(乡科级正职    非领导职务)     正科级或副科级满三年

            //副科长 （乡科级副职   领导职务）      副科级或科员满三年
            //副主任科员(乡科级副职  非领导职务)   副科级或科员蛮三年
            // 且向下兼容 即上级填报下级职务，无限制

            StringBuilder sb = new StringBuilder();
            string nameOrCard = "1=1";
            if (!string.IsNullOrEmpty(tbCondition.Text) && !tbCondition.Text.Equals("请输入姓名或证件号码"))
            {
                nameOrCard = tbCondition.Text;
               // sb.Append(string.Format("emp.OrgID={0} and (emp.Name like '%{1}%' or emp.CardNo like '%{1}%')", _OrgID, nameOrCard)); 
                sb.Append(string.Format(" and (emp.Name like '%{0}%' or emp.CardNo like '%{0}%')", nameOrCard));
            }
            else
            {
               // sb.Append(string.Format("emp.OrgID={0} ", _OrgID));
            }
            if (!_ISsh)
            {
                sb.Append("  and   (");
                if (_MainInfo.PositionLevel.Equals("乡科级正职（主任科员）") && _MainInfo.PositionAttibute.Equals("领导职务"))
                {
                    //正科长   (乡科级正职  领导职务)       正科级或副科级满二年
                    sb.Append("duty.DutyLevel='乡科级正职（主任科员）'  or (duty.DutyLevel='乡科级副职（副主任科员）' and  DATEDIFF(MONTH, duty.JobDate, GETDATE())/12 >=2))");
                }
                else if (_MainInfo.PositionLevel.Equals("乡科级正职（主任科员）") && _MainInfo.PositionAttibute.Equals("非领导职务"))
                {
                    //主任科员(乡科级正职    非领导职务)     正科级或副科级满三年
                    sb.Append("duty.DutyLevel='乡科级正职（主任科员）'  or (duty.DutyLevel='乡科级副职（副主任科员）' and  DATEDIFF(MONTH, duty.JobDate, GETDATE())/12 >=3))");
                }
                else if (_MainInfo.PositionLevel.Equals("乡科级副职（副主任科员）") || _MainInfo.PositionLevel.Equals("乡科级正职或副职"))
                {
                    //副科长 （乡科级副职   领导职务）      副科级或科员满三年
                    //副主任科员(乡科级副职  非领导职务)   副科级或科员蛮三年
                    sb.Append("duty.DutyLevel='乡科级副职（副主任科员）' or  duty.DutyLevel='乡科级正职（主任科员）' or (duty.DutyLevel='科员' and  DATEDIFF(MONTH, duty.JobDate, GETDATE())/12 >=3))");
                }
                else
                {
                  //  sb.Append(" 1=1 )");
                }
            }
            
            


            //switch (_MainInfo.PositionLevel)
            //{
            //    case "乡科级正职（主任科员）":                    
            //        sb.Append("duty.DutyLevel='乡科级正职（主任科员）'  or (duty.DutyLevel='乡科级副职（副主任科员）' and  DATEDIFF(MONTH, duty.RXRZWSXSJ, GETDATE())/12 >=3))");
            //        break;

            //    case "乡科级正职或副职":
            //    case "乡科级副职（副主任科员）":
            //        sb.Append("duty.DutyLevel='乡科级副职（副主任科员）'  or (duty.DutyLevel='科员' and  DATEDIFF(MONTH, duty.RXRZWSXSJ, GETDATE())/12 >=3))");
            //        break;
                   
            //    case "科员":
            //        sb.Append("duty.DutyLevel='科员')");
            //        break;
            //}
           // _Client.GetEmpBasicInfoAccordingToDutyAsync(sb.ToString() + " and emp.EmpStatus='在职' and emp.KeChuJi = 0");
            _Client.GetEmpBasicInfoAccordingToDuty_ZJAsync(sb.ToString() + " and emp.EmpStatus='在职' and emp.KeChuJi = 0",_OrgID);
        }        

        private void ChildWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Button_Click(null, null);
            }
        }

        public ObservableCollection<JinShengService.Emp_BasicInfoModel> SelectItems = new ObservableCollection<JinShengService.Emp_BasicInfoModel>();

        /// <summary>
        /// 破格提拔
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdminSearch_Click(object sender, RoutedEventArgs e)
        {
            string nameOrCard = "1=1";
            if (!string.IsNullOrEmpty(tbCondition.Text) && !tbCondition.Text.Equals("请输入姓名或证件号码"))
            {
                nameOrCard = tbCondition.Text;
              //  _Client.GetEmpBasicInfoAsync(string.Format("OrgID={0} and (Name like '%{1}%' or CardNo like '%{1}%') and EmpStatus='在职'", _OrgID, nameOrCard));
                _Client.GetEmpBasicInfo_zjAsync(string.Format("and (Name like '%{0}%' or CardNo like '%{0}%') and EmpStatus='在职'", nameOrCard), _OrgID);
                // _empClient.GetEmp_BasicInfoAsync(_OrgID + "", "0", "", "", 0, 20, "在职", string.Format("emp.Name like '%{0}%' or emp.CardNo like '%{0}%'", nameOrCard));
            }
            else
            {
               // _Client.GetEmpBasicInfoAsync(string.Format("OrgID={0} and EmpStatus='在职' ", _OrgID));
                _Client.GetEmpBasicInfo_zjAsync(string.Format(" and EmpStatus='在职' "),_OrgID);
            }
        }
       
    }
}

